﻿Imports System.Data.SqlClient

Public Class clsUNIDADES_MEDIDA
    Public Sub GUARDAR_UNIDAD(ByVal CODIGO As String, ByVal NOMBRE As String)
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("sp_UNIDADES_INSERTAR", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@CODIGO", SqlDbType.VarChar, 10))
        Command.Parameters("@CODIGO").Value = CODIGO
        Command.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 100))
        Command.Parameters("@NOMBRE").Value = NOMBRE

        Try
            cnConn.Open()

            Command.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try


    End Sub
    Public Sub INSERTA_CONVERSION(ByVal IDUNIDAD1 As Integer, ByVal IDUNIDAD2 As Integer, ByVal VALOR As Double)
        Dim sql As String = "Insert Into CONVERSIONES (" & _
                           "IDUNIDAD1," & _
                           "IDUNIDAD2," & _
                           "EQUIVALENCIA)" & _
                           "VALUES (" & _
                           "@IDUNIDAD1," & _
                           "@IDUNIDAD2," & _
                           "@EQUIVALENCIA)"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IDUNIDAD1", IDUNIDAD1)
        Command.Parameters.AddWithValue("IDUNIDAD2", IDUNIDAD2)
        Command.Parameters.AddWithValue("EQUIVALENCIA", VALOR)

        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
            If RowsAffected > 0 Then
                cnConn.Close()
            Else
                If cnConn.State = ConnectionState.Open Then
                    cnConn.Close()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
        End Try

    End Sub
    Public Function REGISTRA_UNIDAD(ByVal ABREVIACION As String, ByVal NOMBRE As String, ByVal TIPO As String, ByVal ESTATUS As Integer) As Integer
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("sp_UNIDADES_MEDICION", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 100))
        Command.Parameters("@NOMBRE").Value = NOMBRE
        Command.Parameters.Add(New SqlParameter("@ABREVIACION", SqlDbType.VarChar, 10))
        Command.Parameters("@ABREVIACION").Value = ABREVIACION
        Command.Parameters.Add(New SqlParameter("@TIPO", SqlDbType.VarChar, 100))
        Command.Parameters("@TIPO").Value = TIPO
        Command.Parameters.Add(New SqlParameter("@ESTATUS", SqlDbType.Int))
        Command.Parameters("@ESTATUS").Value = ESTATUS
        Command.Parameters.Add(New SqlParameter("@DECIMALES", SqlDbType.Int))
        Command.Parameters("@DECIMALES").Value = 6
        Command.Parameters.Add(New SqlParameter("@NOMBRE_USUARIO", SqlDbType.VarChar, 50))
        Command.Parameters("@NOMBRE_USUARIO").Value = UsuarioActivo

        Try
            cnConn.Open()
            Dim rdBuscar As SqlDataReader = Command.ExecuteReader()

            If rdBuscar.Read() Then
                REGISTRA_UNIDAD = rdBuscar("RESULTADO")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            REGISTRA_UNIDAD = -1
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try



    End Function
    Public Function DEFINE_CONVERSION(ByVal UNIDAD_PADRE As String, ByVal UNIDAD_HIJO As String, ByVal VALOR As Double, ByVal EQUIVALENCIA As Double, ByVal ESTATUS As Integer) As Integer
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("sp_UNIDADES_CONVERSIONES", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@UNIDAD_PADRE", SqlDbType.VarChar, 100))
        Command.Parameters("@UNIDAD_PADRE").Value = UNIDAD_PADRE
        Command.Parameters.Add(New SqlParameter("@UNIDAD_HIJO", SqlDbType.VarChar, 100))
        Command.Parameters("@UNIDAD_HIJO").Value = UNIDAD_HIJO
        Command.Parameters.Add(New SqlParameter("@VALOR", SqlDbType.Decimal))
        Command.Parameters("@VALOR").Value = VALOR
        Command.Parameters.Add(New SqlParameter("@EQUIVALENCIA", SqlDbType.Decimal))
        Command.Parameters("@EQUIVALENCIA").Value = EQUIVALENCIA
        Command.Parameters.Add(New SqlParameter("@NOMBRE_USUARIO", SqlDbType.VarChar, 50))
        Command.Parameters("@NOMBRE_USUARIO").Value = UsuarioActivo
        Command.Parameters.Add(New SqlParameter("@ESTATUS", SqlDbType.Int))
        Command.Parameters("@ESTATUS").Value = ESTATUS


        Try
            cnConn.Open()
            Dim rdBuscar As SqlDataReader = Command.ExecuteReader()

            If rdBuscar.Read() Then
                DEFINE_CONVERSION = rdBuscar("RESULTADO")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            DEFINE_CONVERSION = -1
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try



    End Function
    Public Function UNIDADES_LOGISTICA(ByVal Nombre As String, ByVal TIPO As Integer, ByVal LARGO As Double, ByVal ALTO As Double, ByVal ANCHO As Double, ByVal PESO As Double) As Integer
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("sp_UNIDADES_LOGISTICA", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 100))
        Command.Parameters("@NOMBRE").Value = Nombre
        Command.Parameters.Add(New SqlParameter("@TIPO", SqlDbType.Int))
        Command.Parameters("@TIPO").Value = TIPO
        Command.Parameters.Add(New SqlParameter("@LARGO", SqlDbType.Decimal))
        Command.Parameters("@LARGO").Value = LARGO
        Command.Parameters.Add(New SqlParameter("@ALTO", SqlDbType.Decimal))
        Command.Parameters("@ALTO").Value = ALTO
        Command.Parameters.Add(New SqlParameter("@ANCHO", SqlDbType.Decimal))
        Command.Parameters("@ANCHO").Value = ANCHO
        Command.Parameters.Add(New SqlParameter("@PESO", SqlDbType.Decimal))
        Command.Parameters("@PESO").Value = PESO
        Command.Parameters.Add(New SqlParameter("@NOMBRE_USUARIO", SqlDbType.VarChar, 50))
        Command.Parameters("@NOMBRE_USUARIO").Value = UsuarioActivo
       


        Try
            cnConn.Open()
            Dim rdBuscar As SqlDataReader = Command.ExecuteReader()

            If rdBuscar.Read() Then
                UNIDADES_LOGISTICA = rdBuscar("RESULTADO")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            UNIDADES_LOGISTICA = -1
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try



    End Function
End Class
